package com.own.toyexchange;

/*
 * 修改个人信息的Activity
 * 用户名不可以修改，其他可修改信息都是显示用户原来的信息，且均不可为空
 */

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.bjtu.toyexchange.R;
import com.main.toyexchange.GerenActivity;
import com.util.toyexchange.DialogUtil;
import com.util.toyexchange.HttpUtil;

public class ModifyInfoActivity extends Activity {
	private String username, password, pwd_ag, phone_num, provice, city, distiact, road, number;
	private TextView tv_username;
	private Spinner provice_sp;
	private ArrayAdapter adapter;
	private Button fanhui, mod_btn;
	private EditText ed_password, ed_pwd_ag, ed_phone_num, ed_city, ed_distiact, ed_road, ed_number;
//===================================================================================================
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mod_info);
        
        //得到用户的用户名、密码、联系方式、地址等信息
        Intent intent = getIntent();
		
		username = intent.getStringExtra("username");
		password = intent.getStringExtra("password");

		//测试数据
		phone_num = "1399999999";
		provice = "河南省";
		city = "郑州";
		distiact = "金水";
		road = "人民";
		number = "6";
		
		//设置用户名textview为用户名
		tv_username = (TextView)ModifyInfoActivity.this.findViewById(R.id.mod_username);
		tv_username.setText(username);
		
		//设置密码、联系方式、地址等edittext为用户的密码、联系方式、地址
		ed_password = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_password);
		ed_password.setText(password);
		ed_pwd_ag = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_password_ag);
		ed_pwd_ag.setText(password);
		ed_phone_num = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_way);
		ed_phone_num.setText(phone_num);
		ed_city = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_city);
		
		ed_city.setText(city);
		ed_distiact = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_distiact);
		ed_distiact.setText(distiact);
		ed_road = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_road);
		ed_road.setText(road);
		ed_number = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_number);
		ed_number.setText(number);
		
		//返回的按钮
		fanhui = (Button)findViewById(R.id.mod_back_btn);
		fanhui.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ModifyInfoActivity.this.finish();
			}
		});
		
		//确认修改的按钮
		mod_btn = (Button)findViewById(R.id.btn_mod);
		mod_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//得到用户输入的信息，如：密码、联系方式等等
				ed_password = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_password);
				password = ed_password.getText().toString();
				
				ed_pwd_ag = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_password_ag);
				pwd_ag = ed_pwd_ag.getText().toString();
				
				ed_phone_num = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_way);
				phone_num = ed_phone_num.getText().toString();
				
				ed_city = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_city);
				city = ed_city.getText().toString();
				
				ed_distiact = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_distiact);
				distiact = ed_distiact.getText().toString();
				
				ed_road = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_road);
				road = ed_road.getText().toString();
				
				ed_number = (EditText)ModifyInfoActivity.this.findViewById(R.id.mod_number);
				number = ed_number.getText().toString();
				
                //校验输入的信息
				if(validate(password,pwd_ag,phone_num,city,distiact,road,number)){
					//检验修改是否成功
					if(updatePro(username,password,phone_num,city,distiact,road,number)){
						Intent intent = new Intent();
					    intent.putExtra("password", password);
					    intent.putExtra("phone_num", phone_num);
					    intent.putExtra("city", city);
					    intent.putExtra("distiact", distiact);
					    intent.putExtra("road", road);
					    intent.putExtra("number", number);
//						intent.setClass(ModifyInfoActivity.this, GerenActivity.class);
//						ModifyInfoActivity.this.startActivity(intent);
						ModifyInfoActivity.this.finish();
					}
				}
				
				
				
			}
		});
		
		provice_sp = (Spinner)ModifyInfoActivity.this.findViewById(R.id.mod_sel_province);
		//将可选内容与ArrayAdapter连接起来
		adapter = ArrayAdapter.createFromResource(ModifyInfoActivity.this, R.array.province_arr, 
				android.R.layout.simple_spinner_item);
		//设置下拉列表的风格
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		provice_sp.setAdapter(adapter);
		//设置下拉菜单的默认值是用户的省
		String temp;
		for (int i = 0;i < 34;i++) {
			temp = (String)adapter.getItem(i);
			if(provice.equals(temp)) {
				provice_sp.setSelection(i);
				break;
			}
		}
		//下拉菜单的事件监听器
		provice_sp.setOnItemSelectedListener(new SpinnerXMLSelectedListener());
		provice_sp.setVisibility(View.VISIBLE);
	}

//校验输入格式===========================================================================================
    private boolean validate(String password,String pwd_ag,String phone_num,String city,String distiact,String road,String number){
    	//修改信息校验
		if (("").equals(password) || null == password) {
			Toast.makeText(ModifyInfoActivity.this,"密 码不能为空...",100).show();
			return false;
		}
		else if (password.length() > 8 || password.length() < 4) {
			Toast.makeText(ModifyInfoActivity.this,"密码长度不符合要求...",100).show();
			return false;
		}
		else if (("").equals(pwd_ag) || null == pwd_ag) {
			Toast.makeText(ModifyInfoActivity.this,"确认密码不能为空...",100).show();
			return false;
		}
		else if (!password.equals(pwd_ag)) {
			Toast.makeText(ModifyInfoActivity.this,"两次输入密码不相同...",100).show();
			return false;
		}
		else if (("").equals(phone_num) || null == phone_num) {
			Toast.makeText(ModifyInfoActivity.this,"手机号码不能为空...",100).show();
			return false;
		}
		else if (("").equals(city) || null == city) {
			Toast.makeText(ModifyInfoActivity.this,"所在市不能为空...",100).show();
			return false;
		}
		else if (("").equals(distiact) || null == distiact) {
			Toast.makeText(ModifyInfoActivity.this,"所在区不能为空...",100).show();
			return false;
		}
		else if (("").equals(road) || null == road) {
			Toast.makeText(ModifyInfoActivity.this,"所在街道不能为空...",100).show();
			return false;
		}
		else if (("").equals(number) || null == number) {
			Toast.makeText(ModifyInfoActivity.this,"街道号不能为空...",100).show();
			return false;
		}
		else 
			return true;
		
    }

//验证修改个人信息是否成功===================================================================================
    private boolean updatePro(String u_name,String password,String phone_num,String city,String distiact,String road,String number){
    	JSONObject jsonObj;
		try
		{
			jsonObj = query(u_name,password,phone_num,city,distiact,road,number);
			// 如果userId 大于0
			if (jsonObj.getInt("userupdateId") > 0)
			{
				DialogUtil.showDialog(this
						, "个人信息修改成功"+username, false);
				return true;
			}
			else
				DialogUtil.showDialog(this
						, "个人信息修改失败", false);
		}
		catch (Exception e)
		{
			DialogUtil.showDialog(this
				, "服务器响应异常，请稍后再试！", false);
			
			e.printStackTrace();
		}

		return false;
    }

//定义发送请求的方法=======================================================================================
 	private JSONObject query(String u_name,String password, String phone_num,String city,String distiact,String road,String number)
 					throws Exception
 				{
 					// 使用Map封装请求参数
 					Map<String, String> map = new HashMap<String, String>();
 					
 					map.put("u_name", u_name);
 					map.put("password", password);
 					map.put("phone_num", phone_num);
 					map.put("province", "zhejiangtest");
 					map.put("city", city);
 					map.put("distiact", distiact);
 					map.put("road", road);
 					map.put("number", number);
 					
 					// 定义发送请求的URL
 					String url = HttpUtil.BASE_URL + "UpdateUserSevlet";
 					// 发送请求
 					JSONObject jb = new JSONObject(HttpUtil.postRequest(url, map)); 
 					
 					return jb;
 				}
 				
    
//===================================================================================================	
class SpinnerXMLSelectedListener implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {			
			provice = (String)adapter.getItem(arg2);
		}
		public void onNothingSelected(AdapterView<?> arg0) {
			
		}
	}

}
